/**
 * Messages class.
 * 
 * @language The language to use.
 * @param successCallback The callback function on success. Invoked when messages have been setup.
 * @param errorCallback The callback function on error.
 */
function Messages(language, successCallback, errorCallback)
{
	// Store these for later use.
	Messages.successCallback = successCallback;
	Messages.errorCallback = errorCallback;
	Messages.language = language;
	
	loquemeServer.Messages.getMessagesMap(Messages.setup, language);
}

/**
 * Sets up the messages used by the application.
 */
Messages.setup = function(result, exception)
{
	if (result != null)
	{
		// If no translation was found then use Google translation API.
        if (result.noTranslation)
        {
            Messages.translateMessages(result.messages.map);
        }
        else
        {
			// Copy messages.
			for (var message in result.messages.map)
			{
				Messages[message] = result.messages.map[message];
			}
			
			// Notify message loading is complete.
            Messages.successCallback();
        }
	}
	else
	{
		// Notify message loading failed.
		Messages.errorCallback();
	}
};

/**
 * Translates the messages.
 * 
 * @messageMap The map of messages.
 */
Messages.translateMessages = function(messageMap)
{
	// Put message keys and values into two arrays.
	var counter = 0;
	var messageKeys = new Array();
	var messageValues = new Array();
	for (var message in messageMap)
    {
    	messageKeys[counter] = message;
    	messageValues[counter] = messageMap[message];
    	counter++;
    }
    
    // Start recursive procedure for loading all messages.
    Messages.translateMessage(messageKeys, messageValues, 0);
};

/**
 * Recursively translate selected message and then move onto next one.
 * 
 * @messageKeys Array of message keys.
 * @messageValues Array of message values.
 * @counter The current message being translated.
 */
Messages.translateMessage = function(messageKeys, messageValues, counter)
{
    google.language.translate(messageValues[counter], 'en', Messages.language,
    function(messageKeys, messageValues, messageValuesJson)
    {
        return function(result)
        {
            if (result.translation)
            {
            	// Insert translated message.
            	Messages[messageKeys[counter]] = result.translation;
            }
            else
            {
                // Copy original message if there was a problem (default is English).
                Messages[messageKeys[counter]] = messageValues[counter];
            }
            
            // Continue or signal success.
            counter++;
            if (counter < messageKeys.length)
            {
            	// Recurse with next message.
            	Messages.translateMessage(messageKeys, messageValues, counter);
            }
            else
            {
                // Notify message loading is complete.
                Messages.successCallback();
            }
        }
    }(messageKeys, messageValues, counter));
};